package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;


public class ConfirmDialog extends Dialog implements View.OnClickListener {
    private Button btnCancel, btnConfirm;
    private TextView tv;
    private Context context;
    private ConfirmDialogCallback callback;
    private int id;

    public ConfirmDialog(@NonNull Context context, ConfirmDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }

    public ConfirmDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View root = LayoutInflater.from(context).inflate(R.layout.confirm_cancel_dialog, null);
        btnCancel = root.findViewById(R.id.btn_cancel);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        tv = root.findViewById(R.id.tv_title);
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }


    public void showDialog(String msg) {
        tv.setText(msg);
        show();
    }
    public void showDialog(String msg,int id) {
        tv.setText(msg);
        this.id=id;
        show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                callback.selectResult(1,id);
                cancel();
                break;
            case R.id.btn_cancel:
                callback.selectResult(-1,id);
                cancel();
                break;
            default:
        }
    }


    public interface ConfirmDialogCallback {
        void selectResult(int value,int id);
    }
}
